package org.feather.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.feather.pojo.Items;
import org.feather.pojo.ItemsImg;
import org.feather.pojo.ItemsParam;
import org.feather.pojo.ItemsSpec;
import org.feather.pojo.vo.CommentLevelCountsVO;
import org.feather.pojo.vo.ItemInfoVO;
import org.feather.pojo.vo.ShopcartVO;
import org.feather.service.ItemService;
import org.feather.utils.FeatherJSONResult;
import org.feather.utils.PagedGridResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: foodie-dev
 * @description:
 * @author: 杜雪松(feather)
 * @create: 2020-05-07 15:09
 **/
@Api(value = "商品接口",tags = {"商品信息展示的相关接口"})
@RestController
@RequestMapping("item")
public class ItemsController  extends  BaseController{
    @Autowired
    private ItemService itemService;

    @ApiOperation(value = "获取商品详情",notes = "查询商品详情",httpMethod = "GET")
    @RequestMapping("/info/{itemId}")
    public FeatherJSONResult info(
            @ApiParam(name="itemId",value = "商品id",required = true)
            @PathVariable String itemId) {
        if (StringUtils.isBlank(itemId)){
            return FeatherJSONResult.errorMsg(null);
        }
        Items items = itemService.queryItemById(itemId);
        List<ItemsImg> itemsImgList = itemService.queryItemImgList(itemId);
        List<ItemsSpec> itemsSpecList = itemService.queryItemSpecList(itemId);
        ItemsParam itemsParam = itemService.queryItemParam(itemId);
        ItemInfoVO itemInfoVO=new ItemInfoVO();
        itemInfoVO.setItem(items);
        itemInfoVO.setItemImgList(itemsImgList);
        itemInfoVO.setItemSpecList(itemsSpecList);
        itemInfoVO.setItemParams(itemsParam);
        return  FeatherJSONResult.ok(itemInfoVO);


    }

    @ApiOperation(value = "查询商品评价等级",notes = "查询商品评价等级",httpMethod = "GET")
    @RequestMapping("/commentLevel")
    public FeatherJSONResult commentLevel(
            @ApiParam(name="itemId",value = "商品id",required = true)
            @RequestParam String itemId) {
        if (StringUtils.isBlank(itemId)){
            return FeatherJSONResult.errorMsg(null);
        }
       CommentLevelCountsVO commentLevelCountsVO = itemService.queryCommentCounts(itemId);
        return  FeatherJSONResult.ok(commentLevelCountsVO);


    }
    @ApiOperation(value = "查询商品评论",notes = "查询商品评论",httpMethod = "GET")
    @RequestMapping("/comments")
    public FeatherJSONResult comments(
            @ApiParam(name="itemId",value = "商品id",required = true)
            @RequestParam String itemId,
            @ApiParam(name="level",value = "评价等级",required = false)
            @RequestParam Integer level,
            @ApiParam(name="page",value = "查询下一页的第几页",required = false)
            @RequestParam Integer page,
            @ApiParam(name="pageSize",value = "分页的每一页显示的条数",required = false)
            @RequestParam Integer pageSize) {
        if (StringUtils.isBlank(itemId)){
            return FeatherJSONResult.errorMsg(null);
        }
        if (page==null){
            page=1;
        }
        if (pageSize==null){
            pageSize=COMMON_PAGE_SIZE;
        }
        PagedGridResult grid = itemService.queryPageComments(itemId, level, page, pageSize);
        return  FeatherJSONResult.ok(grid);


    }
    @ApiOperation(value = "搜索商品列表", notes = "搜索商品列表", httpMethod = "GET")
    @GetMapping("/search")
    public FeatherJSONResult search(
            @ApiParam(name = "keywords", value = "关键字", required = true)
            @RequestParam String keywords,
            @ApiParam(name = "sort", value = "排序", required = false)
            @RequestParam String sort,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {

        if (StringUtils.isBlank(keywords)) {
            return FeatherJSONResult.errorMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }

        PagedGridResult grid = itemService.searhItems(keywords,
                sort,
                page,
                pageSize);

        return FeatherJSONResult.ok(grid);
    }
    @ApiOperation(value = "通过分类ID搜索商品列表", notes = "通过分类ID搜索商品列表", httpMethod = "GET")
    @GetMapping("/catItems")
    public FeatherJSONResult catItems(
            @ApiParam(name = "catId", value = "三级分类id", required = true)
            @RequestParam Integer catId,
            @ApiParam(name = "sort", value = "排序", required = false)
            @RequestParam String sort,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {

        if (catId==null) {
            return FeatherJSONResult.errorMsg(null);
        }

        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }

        PagedGridResult grid = itemService.searhItems(catId,
                sort,
                page,
                pageSize);

        return FeatherJSONResult.ok(grid);
    }
    //用于用户长时间未登陆网站，刷新购物车中的数据（主要是商品价格）
    @ApiOperation(value = "根据商品规格ID查询最新的商品数据", notes = "根据商品规格ID查询最新的商品数据", httpMethod = "GET")
    @GetMapping("/refresh")
    public FeatherJSONResult refresh(
            @ApiParam(name = "itemsSpecIds", value = "拼接的规格ids", required = true,example = "1001,1003,10005")
            @RequestParam String itemsSpecIds) {

        if (StringUtils.isBlank(itemsSpecIds)) {
            return FeatherJSONResult.ok();
        }
        List<ShopcartVO> list = itemService.queryItemsBySpecIds(itemsSpecIds);
        return FeatherJSONResult.ok(list);
    }


}
